package com.xiaoyy.core.common.utils;

import cn.hutool.core.util.CharsetUtil;
import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.xiaoyy.core.common.exception.AppException;
import org.apache.commons.lang3.StringUtils;

/**
 * 拼音工具类<br>
 * 
 * @author chenzh
 */
public class PinyinUtil {
	/**
	 * 获取所给中文的每个汉字首字母组成首字母字符串
	 * 
	 * @param chinese 汉字字符串
	 * @return 首字母字符串
	 */
	public static String getAllFirstLetter(String chinese)  {
		if (StringUtils.isBlank(chinese)) {
			return StringUtils.EMPTY;
		}
        try {
            return PinyinHelper.getShortPinyin(chinese);
        } catch (PinyinException e) {
            e.printStackTrace();
        }
        return StringUtils.EMPTY;
    }

	/**
	 * 汉字转拼音
	 * <hr>
	 * example : 张三 zhangsan
	 * 
	 * @param chinese 汉字
	 * @return 对应的拼音
	 */
	public static String getPinYin(String chinese)  {
		if (StringUtils.isBlank(chinese)) {
			return StringUtils.EMPTY;
		}
        try {
            return PinyinHelper.convertToPinyinString(chinese,"", PinyinFormat.WITHOUT_TONE);
        } catch (PinyinException e) {
            e.printStackTrace();
        }
        return StringUtils.EMPTY;
    }
	/**
	 * 汉字转拼音 格式化
	 *  WITH_TONE_NUMBER--数字代表声调，WITHOUT_TONE--不带声调，WITH_TONE_MARK--带声调
	 * <hr>
	 * example : 张三 zhangsan
	 *
	 * @param chinese 汉字
	 * @return 对应的拼音
	 */
	public static String getPinYin(String chinese,PinyinFormat format)  {
		if (StringUtils.isBlank(chinese)) {
			return StringUtils.EMPTY;
		}
		if (format==null) {
			format = PinyinFormat.WITHOUT_TONE;
		}

        try {
            return PinyinHelper.convertToPinyinString(chinese,"", format);
        } catch (PinyinException e) {
            e.printStackTrace();
        }
        return StringUtils.EMPTY;
    }
	/**
	 * 汉字转拼音 格式化
	 *  WITH_TONE_NUMBER--数字代表声调，WITHOUT_TONE--不带声调，WITH_TONE_MARK--带声调
	 * <hr>
	 * example : 张三 zhangsan
	 *@param separator 分隔符
	 * @param chinese 汉字
	 * @return 对应的拼音
	 */
	public static String getPinYin(String chinese,String separator,PinyinFormat format) {
		if (StringUtils.isBlank(chinese)) {
			return StringUtils.EMPTY;
		}
		if (StringUtils.isBlank(separator)) {
			separator="";
		}
		if (format==null) {
			format = PinyinFormat.WITHOUT_TONE;
		}
        try {
            return PinyinHelper.convertToPinyinString(chinese,separator, format);
        } catch (PinyinException e) {
            e.printStackTrace();
        }
        return StringUtils.EMPTY;
    }
	/**
	 * 繁体转换为简体
	 * @param str
	 * @return
	 */
	public static String convertToSimpli(String str){
		String convertToSimplifiedChinese ="";

		if(!(null ==str || "".equals(str.trim()))){

			convertToSimplifiedChinese = ChineseHelper.convertToSimplifiedChinese(str);
		}

		return convertToSimplifiedChinese;

	}
	/**
	 * 简体转换为繁体
	 * @param str
	 * @return
	 */
	public static String convertToTradit(String str){
		String convertToSimplifiedChinese ="";

		if(!(null ==str || "".equals(str.trim()))){

			convertToSimplifiedChinese = ChineseHelper.convertToTraditionalChinese(str);
		}

		return convertToSimplifiedChinese;

	}


	/**
	 * 判断字符是否是多音字， 是--true  ， 不是--false
	 * @param str
	 * @return
	 */
	public static boolean hasMushPinyin(String str ){
		char charAt = 0;
		if(null == null || "".equals(str.trim())){
			charAt = str.charAt(0);
		}
		boolean flag = PinyinHelper.hasMultiPinyin(charAt);
		return flag;
	}
	/**
	 * 获取汉字对应的ascii码
	 * @param chs 汉字
	 * @return ascii码
	 */
	private static int getChsAscii(String chs) {
		int asc = 0;
		byte[] bytes = chs.getBytes(CharsetUtil.CHARSET_GBK);
		switch (bytes.length) {
		case 1:
			// 英文字符
			asc = bytes[0];
			break;
		case 2:
			// 中文字符
			int hightByte = 256 + bytes[0];
			int lowByte = 256 + bytes[1];
			asc = (256 * hightByte + lowByte) - 256 * 256;
			break;
		default:
			throw new AppException("Illegal resource string");
		}
		return asc;
	}
	//------------------------------------------------------------------------------------------------------- Private method end
}
